<template>
	<view class="content">
		<!-- 开机画面 -->
		<image :src="staticUrl+'/image/gif/bootAnimation.gif'"></image>
	</view>
</template>

<script>
	const app = getApp();
	import * as api from '@/api/login';
	import {
		isNull
	} from '@/utils/utils.js';
	export default {
		data() {
			return {
				staticUrl: this.$config.StaticUrl,
				innerAudioContext: null,
				timer: null,
				timeout: 3600
			};
		},
		onLoad() {
			// 在此处判断用户是否需要重新登录
			let that = this;
			// try {
			let userInfo = this.$store.state.userInfo;
			console.log(userInfo);
			if (isNull(userInfo)) {
				let timeout = that.timeout;
				that.timer = setTimeout(function() {
					console.log('去登陆选择页面');
					that.toLoginWay()
					clearTimeout(that.timer);
				}, timeout);
				return;
			}
			let currentUser = userInfo.currentUser;
			let appId = currentUser.appId;
			let appSecret = currentUser.appSecret;
			let userId = currentUser.userId;
			if (isNull(appId) && isNull(appSecret) && isNull(userId)) {
				console.log('都为空，去登录。。。');
				// 跳转到登录页
				let timeout = that.timeout;
				that.timer = setTimeout(function() {
					that.toLoginWay()
					clearTimeout(that.timer);
				}, timeout);
			} else {
				console.log('更新 appSecret');
				let UUID = that.$store.state.deviceUUID;
				let data = {
					userId: userId,
					action: 'updateAppSecret',
					params: {
						appId: appId,
						appSecret: appSecret,
						uuid: UUID
					}
				};
				api.updateAppSecret(data).then(function(res) {
					console.log('updateAppSecret success', res);
					if (res.data.code == 2000) {
						try {
							that.$store.commit('token', res.data.data.token);
							if (isNull(that.$store.state.userInfo)) {
								that.timer = setTimeout(function() {
									that.toLoginWay()
									clearTimeout(that.timer);
								}, that.timeout);
								return;
							}
							// 每次登录app  appsecret只更新到当前用户信息中去???
							that.$store.commit('UpdateAppSecret', res.data.data.appSecret);
							//开始轮询发送tokenId 请求
							app.getTokenIdPolling();
							// 获取用户信息
							that.getUserInfo();
						} catch (e) {
							// error
							console.log('fail', e);
						}
					} else if (res.data.code == 2001) {
						that.timer = setTimeout(function() {
							that.toLoginWay()
							clearTimeout(that.timer);
						}, that.timeout);
					}
				}).catch(function(error) {
					console.log('updateAppSecret fail', error);
				});
			}
		},
		onReady() {
			// 定义一个音频
			this.innerAudioContext = uni.createInnerAudioContext();
			this.innerAudioContext.autoplay = false;
			this.innerAudioContext.loop = false;
			this.innerAudioContext.src = this.$config.StaticUrl + '/audio/bgm.mp3';
			this.innerAudioContext.play();
		},
		methods: {
			// 选择登陆方式
			toLoginWay() {
				uni.redirectTo({
					url: '/pages/login/loginWay/loginWay'
				})
			},
			toLogin() {
				uni.redirectTo({
					url: '../login/login'
				});
			},
			getUserInfo() {
				let that = this;
				let userId = this.$store.state.userInfo.currentUser.userId;
				let data = {
					userId: userId,
					action: 'getUserInfo'
				};
				api.getUserInfo(data).then(function(res) {
					// console.log('getUserInfo::', res);
					if (res.data.message == 'userInfo is null') {
						that.timer = setTimeout(function() {
							uni.redirectTo({
								url: '../login/fillHealhyRecords/noInfo'
							});
							clearTimeout(that.timer);
						}, that.timeout);
					} else {
						that.$store.commit('userBaseInfo', res.data.data)
						that.timer = setTimeout(function() {
							clearTimeout(that.timer)
							uni.switchTab({
								url: '/pages/detection/detection'
							})
						}, that.timeout)
					}
				}).catch(function(error) {
					console.log('get userInfo fail', error);
				});
			},
		}
	};
</script>

<style>
	page {
		background-color: #f5f5f5;
	}

	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		width: 100vw;
		height: 100vh;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	image {
		width: 100vw;
		height: 100vh;
	}
</style>